#ifndef GY_COLORUSERDATA_H
#define GY_COLORUSERDATA_H

#endif // GY_COLORUSERDATA_H

#include <QObject>
#include "../../LuaInterpreter/lua.hpp"

struct Color {
    double r;
    double g;
    double b;
    double a;
    //运算符
};


// 定义Color元表的__index元方法
int color_index(lua_State* L) {
    Color* p = static_cast<Color*>(lua_touserdata(L, 1));
    const char* key = lua_tostring(L, 2);
    if (strcmp(key, "r") == 0) {
        lua_pushinteger(L, p->r);
    } else if (strcmp(key, "g") == 0) {
        lua_pushinteger(L, p->g);
    } else if (strcmp(key, "b") == 0) {
        lua_pushinteger(L, p->b);
    } else if (strcmp(key, "a") == 0) {
        lua_pushinteger(L, p->a);
    } else {
        lua_pushnil(L);
    }
    return 1;
}

// 定义Color元表的__newindex元方法
int color_newindex(lua_State* L) {
    Color* p = static_cast<Color*>(lua_touserdata(L, 1));
    const char* key = lua_tostring(L, 2);
    if (strcmp(key, "r") == 0) {
        p->r = lua_tointeger(L, 3);
    } else if (strcmp(key, "g") == 0) {
        p->g = lua_tointeger(L, 3);
    } else if (strcmp(key, "b") == 0) {
        p->b = lua_tointeger(L, 3);
    } else if (strcmp(key, "a") == 0) {
        p->a = lua_tointeger(L, 3);
    }
    return 0;
}

